# plugs/loglevel.py
#
#

""" get/set loglevel """

__copyright__ = 'this file is in the public domain'

from ommbot.world import add_cmnd, add_example, log, get_config

def handle_loglevel(bot, ievent):
    """ loglevel <level> .. get or set loglevel .. the lower the more the \
        bot logs """
    if len(ievent.args) == 0:
        handle_loglevel2(bot, ievent)
        return
    try:
        level = int(ievent.args[0])
    except IndexError:
        ievent.reply('i need a loglevel argument')
        return
    except ValueError:
        ievent.reply('i need a integer argument')
        return
    log.setlevel(level)
    config = get_config()
    if config:
        config.set('loglevel', level)
    ievent.reply('loglevel is now %s' % level)

def handle_loglevel2(bot, ievent):
    """ show loglevel """
    ievent.reply('loglevel is %s' % log.loglevel)

add_cmnd('loglevel', handle_loglevel,'OPER')
add_example('loglevel', 'get/set current loglevel', '1) loglevel 2) \
                 loglevel 0')
